package com.scoe.gcs.comms.activites;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;

import android.app.Activity;
import android.content.Intent;
import com.MAVLink.MAVLink;
import com.MAVLink.Messages.IMAVLinkMessage;
import com.MAVLink.Messages.common.msg_global_position_int;
import com.MAVLink.Messages.common.msg_gps_raw;
import com.MAVLink.Messages.common.msg_gps_raw_int;
import com.MAVLink.Messages.common.msg_request_data_stream;
import com.scoe.gcs.comms.CommonSettings;
import com.scoe.gcs.comms.CommsProxy;
import com.scoe.gcs.comms.AC1Data.GPSData;
import com.scoe.gcs.comms.AC1Data.ProtocolParser;
import com.scoe.gcs.comms.CommunicationClient;
import com.google.android.maps.GeoPoint;

public class GPSActivity{

	boolean firstGPS = true;
	
	public GeoPoint lastPos = null;
	private Activity parent;
	
	private CommsProxy commsProxy = new CommsProxy();
	private KrollDict props = new KrollDict();
	
	private TiApplication appContext = TiApplication.getInstance();
	private Activity rootActivity = appContext.getCurrentActivity();
	
	public GPSActivity(Activity activity) {

		parent = activity;
	    ba.init();
	    
	}
		
	protected void onDestroy() {

		ba.onDestroy();		
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		ba.onActivityResult(requestCode, resultCode, data);
		
	}
	
	
	CommunicationClient ba = new CommunicationClient(rootActivity) {

		String status;
		
		@Override
		public void notifyConnected() {
			if( CommonSettings.isProtocolAC1())
				ba.sendBytesToComm(ProtocolParser.requestGPSData());
			else if (CommonSettings.isProtocolMAVLink()){
				msg_request_data_stream req = new msg_request_data_stream();
				req.req_message_rate = 1;
				
				
				req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_POSITION;
				//req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_EXTENDED_STATUS;
				req.start_stop = 1;
				req.target_system = MAVLink.CURRENT_SYSID;
				req.target_component = 0;
				ba.sendBytesToComm( MAVLink.createMessage(req));
				
			}
		}

		@Override
		public void notifyDisconnected() {
			
		}

		@Override
		public void notifyDeviceNotAvailable() {
			
		}
		
		@Override
		public void notifyReceivedData(int count, IMAVLinkMessage m) {

			if( CommonSettings.isProtocolAC1()){
				GPSData gd = (GPSData)m;
				if(gd.longitude != 0){
					GeoPoint point = new GeoPoint(gd.latitude, gd.longitude);
				
					if(firstGPS){
						firstGPS = false;	
					}
				}			
			}else if (CommonSettings.isProtocolMAVLink()){
				switch(m.messageType){
				
					case msg_global_position_int.MAVLINK_MSG_ID_GLOBAL_POSITION_INT:{
						msg_global_position_int msg = (msg_global_position_int) m;
					
						
//						GeoPoint point = new GeoPoint((int)msg.lat,(int) msg.lon);
//					
//						if(firstGPS){
//							mapController.setCenter(point);
//							firstGPS = false;
//							
//						}
						
						commsProxy.notifyTitaniumGPSRawInt(msg.lat, msg.lon, msg.alt, msg.hdg, msg.vz, status);
						
						break;
					}
					case msg_gps_raw.MAVLINK_MSG_ID_GPS_RAW:{
						msg_gps_raw msg = (msg_gps_raw) m;
		
						if( msg.fix_type > 1){						
						
							if(firstGPS){
								firstGPS = false;	
							}
							
						}
						
						break;
					}
					
					case msg_gps_raw_int.MAVLINK_MSG_ID_GPS_RAW_INT:{
						msg_gps_raw_int msg = (msg_gps_raw_int) m;

						switch(msg.fix_type){
							case 0:
							case 1:
	                            status="No Fix";
	                            break;
	                            
	                        case 2:
	                        	status="2D Fix";
	                            break;

	                        case 3:
	                        	status="3D Fix";
	                            break;
	                        
	                        default:
	                        	status="No GPS";     
						}
						commsProxy.notifyTitaniumGPSRawInt(msg.lat, msg.lon, msg.alt, 0, 0, status);

						break;
					}
				}		
			}
		}
	};
	

	
}
